package operation;

import book.BookList;

import java.util.Scanner;

public class DelOperation extends SearchOperation implements IOperation{

    public void removeBook(BookList bookList,int pos) {
        int usedSize = bookList.getUsedSize();
        for(int i=pos;i<usedSize-1;i++) {
            bookList.setBook(i,bookList.getBook(i+1));
        }
        bookList.setBook(usedSize-1,null);
        bookList.setUsedSize(usedSize-1);
    }

    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书名: ");
        String bookName = scanner.nextLine();
        int index = search(bookList, bookName);
        if(index!=-1) {
            System.out.println("您确定要删除《"+bookName+"》吗? 0.取消 1.确定");
            int choice = scanner.nextInt();
            if(choice == 1) {
                removeBook(bookList, index);
                System.out.println("成功删除《" + bookName + "》!");
            }
        } else {
            System.out.println("未查找到书名为:《"+bookName+"》的图书!");
        }
    }
}
